<?php

namespace App\Admin\Controllers;

use App\Models\Category;
use App\Models\Topic;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;

class TopicController extends Controller
{
    use HasResourceActions;

    /**
     * Index interface.
     *
     * @param Content $content
     * @return Content
     */
    public function index(Content $content)
    {
        return $content
            ->header('话题列表')
            ->body($this->grid());
    }

    /**
     * Show interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function show($id)
    {
        return redirect()->route('topics.show', [$id]);
    }

    /**
     * Edit interface.
     *
     * @param mixed $id
     * @param Content $content
     * @return Content
     */
    public function edit($id, Content $content)
    {
        return $content
            ->header('编辑话题')
            ->body($this->form()->edit($id));
    }


    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        $grid = new Grid(new Topic);

        $grid->id('Id');
        $grid->title('话题标题');
        $grid->user()->name('发表用户');
        $grid->category()->name('话题分类');
        $grid->reply_count('回复次数');
        $grid->view_count('查看次数');
        $grid->created_at('创建日期');

        return $grid;
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        $form = new Form(new Topic);

        $categories = Category::query()->pluck('name','id');
        $form->text('title', '话题标题');
        $form->text('slug', 'Slug');
        $form->select('category_id','话题分类')->options($categories);
        $form->textarea('excerpt', '话题摘要');
        $form->editor('body', '话题内容');

        $form->saving(function (Form $form) {
            //...
        });
        return $form;
    }
}
